package com.linewow.xhyy.nihaonew.activity.account;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.linewow.xhyy.nihaonew.R;
import com.linewow.xhyy.nihaonew.application.NiHaoApp;
import com.linewow.xhyy.nihaonew.base.BaseActivity;
import com.linewow.xhyy.nihaonew.base.SimpleBaseEntity;
import com.linewow.xhyy.nihaonew.entity.db.AppInfo;
import com.linewow.xhyy.nihaonew.db.NiHaoHelper;
import com.linewow.xhyy.nihaonew.event.MessageEvent;
import com.linewow.xhyy.nihaonew.net.api.NiHaoOkgo;

import org.simple.eventbus.EventBus;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by LXR on 2016/9/20.
 */
public class RegisterActivity extends BaseActivity {
    @Bind(R.id.toolbar_back_title)
    TextView toolbarBackTitle;
    @Bind(R.id.toolbar_back_left)
    ImageView toolbarBackLeft;
    @Bind(R.id.toolbar_back_toolbar)
    Toolbar toolbarBackToolbar;
    @Bind(R.id.register_logo_img)
    ImageView registerLogoImg;
    @Bind(R.id.register_email_edit)
    EditText registerEmailEdit;
    @Bind(R.id.register_nickname_edit)
    EditText registerNicknameEdit;
    @Bind(R.id.register_pwd1_edit)
    EditText registerPwd1Edit;
    @Bind(R.id.register_pwd2_edit)
    EditText registerPwd2Edit;
    @Bind(R.id.register_edit_line)
    LinearLayout registerEditLine;
    @Bind(R.id.register_btn)
    RelativeLayout registerBtn;
    @Bind(R.id.widget_protocol_tv)
    TextView widgetProtocolTv;
    @Bind(R.id.register_mask)
    FrameLayout registerMask;

    private String TAG="RegisterActivity";

    @Override
    public void initList() {
        super.initList();
    }

    @Override
    public void initView() {
        super.initView();
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        toolbarBackTitle.setText(getString(R.string.title_register));
//        registerMask.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                if(event.getAction()==MotionEvent.ACTION_DOWN){
//                    hideSoftKeyboard();
//                }
//                return false;
//            }
//        });
        clickHide();
    }

    @Override
    public void initData() {
        super.initData();
    }



    @OnClick({R.id.toolbar_back_left, R.id.register_btn, R.id.widget_protocol_tv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_back_left:
                finish();
                break;
            case R.id.register_btn:
                Log.e(TAG,"点击了");
                String email=registerEmailEdit.getText().toString();
                String pwd1=registerPwd1Edit.getText().toString();
                String pwd2=registerPwd2Edit.getText().toString();
                String nick=registerNicknameEdit.getText().toString();
                if(email.length()<3||pwd1.length()<3||pwd2.length()<3||nick.length()<2||!pwd1.equals(pwd2)){
                    Log.e(TAG,"格式不对");
                    showCanDialog("请填写完整信息");
                }else{
                    Log.e(TAG,"开始请求了");

                    showLoading();
                    NiHaoOkgo.registerInfo(email,nick,pwd1,pwd2)
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(new Action1<SimpleBaseEntity<String>>() {
                                @Override
                                public void call(SimpleBaseEntity<String> stringSimpleBaseEntity) {
                                    dismissLoading();
                                    if(stringSimpleBaseEntity.status){
                                        AppInfo appInfo=new AppInfo(stringSimpleBaseEntity.token,stringSimpleBaseEntity.uid);
                                        NiHaoHelper.getInstance().login(appInfo);
                                        ((NiHaoApp)getApplication()).resumeData();
                                        EventBus.getDefault().post(new MessageEvent("login"),"login");
                                    }else{
                                        showCanDialog(stringSimpleBaseEntity.info);
                                    }
                                }
                            });
                }
                break;
            case R.id.widget_protocol_tv:
                Intent intent=new Intent(RegisterActivity.this,ProtocolActivity.class);
                startActivity(intent);
                break;
        }
    }
}
